﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lizk.SimpleHUD;
using MediaData;

namespace SharpMedia.Controls
{
    public class ScraperIcon : HUDImage
    {
        public MediaDataSet.TitlesRow Title { get; set; }
        
        private ScraperHandler.ScraperStatus status = ScraperHandler.ScraperStatus.None;
        private bool pulse;

        private string defaultImage = null;

        public ScraperIcon(MediaDataSet.TitlesRow title, string defaultImage)
            : base(defaultImage)
        {
            this.defaultImage = defaultImage;
            this.Title = title;
            Alpha = 255;
            UpdateInfo();
            SetIcon();

            this.FadingStopped += new EventHandler(w_FadingStopped);
        }

        
        public ScraperIcon(MediaDataSet.TitlesRow title)
            : base("web.png")
        {
            this.Title = title;
            Alpha = 0;
            UpdateInfo();
            SetIcon();

            this.FadingStopped += new EventHandler(w_FadingStopped);
        }

        private void UpdateInfo()
        {
            ScraperHandler.ScraperState state = SharpMedia.ScraperHandler.Get(Title);
            if (state == null)
            {
                status = ScraperHandler.ScraperStatus.None;
                return;
            }

            status = state.Status;
        }

        private void PulseWebIcon(bool toggle)
        {
            pulse = toggle;
            Fade(toggle ? (byte)255 : (byte)0, 2f);
        }

        private void w_FadingStopped(object sender, System.EventArgs e)
        {
            if (!pulse)
                return;
            if (Alpha == 255)
                Fade(100, 1f);
            else
                Fade(255, 1f);
        }
        public override void Update(TimeSpan ts)
        {
            base.Update(ts);
            ScraperHandler.ScraperStatus oldStatus = status;
            UpdateInfo();
            if(oldStatus != status)
                SetIcon();
        }

        private void SetIcon()
        {


            switch (status)
            {
                case ScraperHandler.ScraperStatus.None:
                    if(defaultImage == null)
                    Fade(0, 2f);
                    else
                    {
                        Fade(255, 2f);
                        File = defaultImage;
                    }
                    break;
                case ScraperHandler.ScraperStatus.InProgress:
                    File = "web.png";
                    PulseWebIcon(true);
                    Fade(255, 2f);
                    break;
                case ScraperHandler.ScraperStatus.MultipleResults:
                    File = "important.png";
                    PulseWebIcon(false);
                    Fade(255, 2f);
                    break;
                case ScraperHandler.ScraperStatus.Error:
                    File = "error.png";
                    PulseWebIcon(false);
                    Fade(255, 2f);
                    break;
                case ScraperHandler.ScraperStatus.Done:
                    if (defaultImage == null)
                    {
                        File = "yes.png";
                        PulseWebIcon(false);
                        Fade(0, 2f);
                    }
                    else
                    {
                        File = defaultImage;
                        PulseWebIcon(false);
                        Fade(255, 2f);
                    }
                    break;
                default:
                    break;
            }
        
        }

    }
}
